﻿using System;
using System.Collections.Generic;
using System.Linq;
using MMSWebsite.Controllers;
using MMSWebsite.DataAccessManagement;
using MMSWebsite.DataAccessManagement.AssetManagement;
using MMSWebsite.HtmlHepler;
using MMSWebsite.Models;

namespace MMSWebsite.Business
{
    public class FVAllocation
    {
        readonly MMSEntities _entities = new MMSEntities();
        readonly EmployeeAccessProvider _eap = new EmployeeAccessProvider();
        CustomerAccessProvider _cap = new CustomerAccessProvider();
        AssetProvider _ap = new AssetProvider();
        
        //Auto Allocate
        public List<Customer> AutoAllocate(List<Customer> customers, List<EmployeeViewModel> empFV  )
        {
            //counter qualtity customers & FV
            int cusCounter = customers.Count();
            int fvCounter = empFV.Count;
            var dictionaryFV = SetDictionary(empFV);

            //ex: 5 FV, 15 Cus
            int i = 1;
            foreach (var customer in customers)
            {
                customer.ResponsibleEmp = dictionaryFV.Where(x=>x.Key == i).Select(r=>r.Value).FirstOrDefault();
                if(i < fvCounter)
                {
                    i++;
                }
                else
                {
                    i = 1;
                }
            }
            return customers;
        }

        //Solo Allocate
        public void FvAllocate(int cusId, string responsibileFV, DateTime? startDay, DateTime? endDay)
        {
            var fv = _eap.GetEmployeeByName(responsibileFV);
            int empId = fv.Employee.Id;
            var cus = _entities.MMS_EMP_CUS_CTRLs.FirstOrDefault(x => x.idCus == cusId);
            if(cus!=null)
            {
                cus.idEmp = empId;
                cus.startDate = startDay;
                cus.endDate = endDay;
                _entities.SaveChanges();
            }
            
            var reviewer = _eap.GetEmployeeByCode(AccountController.EmpAccount.Employee.Code);
            var customer = _cap.GetCusById(cusId);
            WebMailHelper.SendCaseToFV(fv, reviewer, customer);
        }

        //Remove Reponsibility when Reviewer back Customer to Allocation
        public void RemoveResponsibility(int cusID)
        {
            var relationCus = _entities.MMS_EMP_CUS_CTRLs.FirstOrDefault(x => x.idCus == cusID);
            if (relationCus != null)
            {
                relationCus.idEmp = null;
                relationCus.startDate = null;
                relationCus.endDate = null;
            }
            _entities.SaveChanges();
        }

        public Dictionary<int, EmployeeViewModel> SetDictionary(List<EmployeeViewModel> emps )
        {
            var dictionary = new Dictionary<int, EmployeeViewModel>();
            int i = 1;
            foreach (var emp in emps)
            {
                dictionary.Add(i, emp);
                i++;
            }
            return dictionary;
        }
    }
}